package com.gitee.cui.concurrency.designmodel.masterworker;

import lombok.Data;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;

/**
 * @author cuiqingqiang
 * @version 1.0
 * @date 2021/12/8 15:53
 **/
@Data
public class Task<R> {

    /**
     * 序号定义
     */
    private static final AtomicInteger index = new AtomicInteger(1);

    /**
     * 回调方法
     */
    private Consumer<Task<R>> resultAction;

    /**
     * 任务id
     */
    private int id;

    /**
     * 工作者id
     */
    private int workerId;

    /**
     * 结果
     */
    private R result;

    public Task() {
        this.id = index.getAndIncrement();
    }

    public void execute() {
        this.result = this.doExecute();
        this.resultAction.accept(this);
    }

    /**
     * 需要子类实现的方法
     * @return
     */
    protected R doExecute() {
        return null;
    }
}
